#include "jniSurfaceView.h"
#include "jniHelper.h"
#include "plasma.h"

ANativeWindow *Android_JNI_GetNativeWindowS(jobject surface) {
    ANativeWindow *anw;
    JNIEnv *env = Android_JNI_GetEnv();
    anw = ANativeWindow_fromSurface(env, surface);
    return anw;
}


/*
 * Class:     com_undatech_opaque_JniSurfaceView
 * Method:    attachSurface
 * Signature: (Landroid/view/Surface;)V
 */

JNIEXPORT void JNICALL JniSurfaceView_attachSurface
        (JNIEnv *mEnv, jobject thiz, jobject surface) {
    LOGE("Java_com_oe_vdi_JniSurfaceView_attachSurface");
    ANativeWindow *win = Android_JNI_GetNativeWindowS(surface);
    ANativeWindow_setBuffersGeometry(win, 0, 0, 5);
    setNativeWindow(win);
}


/*
 * Class:     com_undatech_opaque_JniSurfaceView
 * Method:    detachSurface
 * Signature: ()V
 */
JNIEXPORT void JNICALL JniSurfaceView_detachSurface
        (JNIEnv *mEnv, jobject j) {
    LOGE("Java_com_oe_vdi_JniSurfaceView_detachSurface");
    setNativeWindow(NULL);
}

JNIEXPORT void JNICALL JniSurfaceView_reDraw
        (JNIEnv *mEnv, jobject j) {
    ANativeWindow *window = JniAquireWindow();

    if (window == NULL) {
        JniReleaseWindow();
        return;
    }

    ANativeWindow_Buffer buffer;
    if (ANativeWindow_lock(window, &buffer, NULL) < 0) {
        LOGW("Unable to lock window buffer");
        return;
    }

    /* Now fill the values with a nice little plasma */
    fill_plasma(buffer.bits, buffer.width, buffer.height, buffer.stride);
    ANativeWindow_unlockAndPost(window);

    JniReleaseWindow();
}



